//==============================================================================
//
//               Copyright (C) 2002  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2006-07-02 05:51:31 $ $Revision: 1.2 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DBitArray.h"
#include "ofc/DFSM.h"

#include "DInc.h"
#include "DTest.h"

//-Misc------------------------------------------------------------------------

void DFSM_test()
{
  DFSMState *start = [DFSMState new];
  DFSMState *digit = [DFSMState new];
  DFSMState *dot   = [DFSMState new];
  DFSMState *rem   = [DFSMState new];
  DFSMState *done  = [DFSMState new];
  
  DFSM      *fsm   = [DFSM      alloc];
  DBitArray *trg   = [DBitArray alloc];
  
  STARTTEST();

  [fsm init];
  [trg init :0 :255];

  [trg reset];
  [trg set  :'0' :'9'];
  [trg set  :'-'];
  [fsm transition :start :[trg copy] :digit];

  [trg reset];
  [trg set :'0' :'9'];
  [fsm transition :digit :[trg copy] :digit];

  [trg reset];
  [trg set :'.'];
  [fsm transition :digit :[trg copy] :dot];

  [trg reset];
  [trg set :'0' :'9'];
  [fsm transition :dot :[trg copy] :rem];

  [fsm transition :rem :[trg copy] :rem];

  [fsm transition :rem :nil        :done];
  
  TEST([fsm start :start] == start);
  TEST([fsm feed  :'a']   == nil);                 // error test
  TEST([fsm start :start] == start);
  
  TEST([fsm feed  :'-']   == digit); TEST([fsm isChanged]);
  TEST([fsm current]      == digit);
  TEST([fsm previous]     == start);
 
  TEST([fsm feed  :'0']   == digit); TEST(![fsm isChanged]);
  TEST([fsm try   :'-']   == nil);   TEST(![fsm isChanged]);

  TEST([fsm try   :'.']   == dot);
  TEST([fsm feed  :'.']   == dot);
  TEST([fsm try   :'.']   == nil);

  TEST([fsm feed  :'9']   == rem);

  TEST([fsm feed  :'5']   == rem);

  TEST([fsm feed  :'6']   == rem);

  TEST([fsm feed  :' ']   == done);
  
  STOPTEST();

  [trg free];
  [fsm free];
}

